<template>
 

    <el-container style="height: auto; border: 1px solid #eee">
      <el-header style="background-color: rgb(238, 241, 246);">
        信息管理系统
      </el-header>
  
      <el-container>
        <el-container>
          <div>
            <el-tree :data="treeData" :props="treeProps" @node-click="handleNodeClick"></el-tree>
          </div>
        </el-container>
  
        <el-aside width="200px" style="background-color: rgb(238, 241, 246);">
        
          <el-menu style="height: 100%;">
            <el-submenu index="1">
              <template slot="title"><i class="el-icon-message"></i>选择学科</template>
            </el-submenu>
          </el-menu>
        </el-aside>
  
        <el-main>
          <div>
            <div class="toolbar left">
              <el-button type="primary" @click="queryStudents">查询学生信息</el-button>
              <el-button type="primary" icon="el-icon-search">搜索学生</el-button>
            </div>
  
            <div class="toolbar right">
              <el-input v-model="searchText" icon="el-icon-search" placeholder="请输入关键字" @keyup.enter.native="searchStudents" />
            </div>
  
            <div class="toolbar right">
              <el-button type="primary" icon="el-icon-download" @click="downloadStudents">下载为Excel</el-button>
              <el-button type="primary" icon="el-icon-search">搜索</el-button>
            </div>
  
            <el-table :data="students" v-loading="loading" :header-cell-style="{ 'text-align': 'center' }"
              :cell-style="{ 'text-align': 'center' }">
              <el-table-column prop="sid" label="学号" width="80"></el-table-column>
              <el-table-column prop="sname" label="姓名" width="100"></el-table-column>
              <el-table-column prop="sage" label="年龄" width="100"></el-table-column>
              <el-table-column prop="ssex" label="性别" width="100"></el-table-column>
  
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteStudent(scope.row)">删除</el-button>
                  <el-button type="primary" size="mini" icon="el-icon-plus" @click="dialogVisible = true">添加学生</el-button>
                </template>
              </el-table-column>
            </el-table>
  
            <div class="pagination-container" v-show="showPagination">
              <el-pagination background layout="prev, pager, next" :current-page="currentPage" :page-size="pageSize"
                :total="totalPages * pageSize" @current-change="changePage">
              </el-pagination>
              <div class="pagination-select">
                <span>每页显示：</span>
                <el-select v-model="pageSize" @change="selectPageSize">
                  <el-option label="4" :value="4"></el-option>
                  <el-option label="6" :value="6"></el-option>
                  <el-option label="8" :value="8"></el-option>
                </el-select>
              </div>
            </div>
          </div>
  
          <!-- 添加学生对话框 -->
          <el-dialog :visible.sync="dialogVisible">
            <add-student @submit="handleSubmit" @cancel="handleCancel" />
          </el-dialog>
  
          <!-- 编辑框 -->
          <el-dialog title="编辑学生" :visible.sync="dialogFormVisible">
            <el-form :model="editStudent">
              <el-form-item label="姓名">
                <el-input v-model="editStudent.sname" style="width: 70%"></el-input>
              </el-form-item>
              <el-form-item label="年龄">
                <el-input v-model="editStudent.sage" style="width: 70%"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-input v-model="editStudent.ssex" style="width: 70%"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取消</el-button>
              <el-button type="primary" @click="handleEditStudent">确定</el-button>
            </div>
          </el-dialog>
        </el-main>
      </el-container>
    </el-container>
  </template>
  <script>
  
  
  import AddStudent from '../views/AddStudent.vue'; // 引入添加学生子组件
  import OrgTree from '@/components/OrgTree.vue';
  export default {
    name: 'IndexView', // 父组件的名称
    components: {
      AddStudent, // 注册子组件
      OrgTree
    },
    data() {
      return {
        editStudent: {},
        students: [],
        totalPages: 0,
        currentPage: 1,
        pageSize: 4,
        loading: false,
        dialogVisible: false,// 控制添加学生对话框的显示/隐藏
        dialogFormVisible: false,// 控制编辑学生对话框的显示/隐藏
        searchText: "",
  
      }
    },
    created() {
      this.editStudent = {
        sid: null,
        sname: "",
        sage: "",
        ssex: "",
      }
    },
    computed: {
      showPagination() {
        return this.totalPages > 1
      }
    },
    methods: {
      async selectPageSize() {
        this.queryStudents();
      },
      async queryStudents() {
        this.loading = true;
        try {
          const response = await this.$axios.get(`/getStudents?page=${this.currentPage}&pageSize=${this.pageSize}`);
          const { data, totalPages } = response.data;
          this.students = data;
          this.totalPages = totalPages;
        } catch (error) {
          console.log(error);
        } finally {
          this.loading = false;
        }
      },
      async downloadStudents() {
        try {
          const response = await this.$axios.get('/download', { responseType: 'blob' });
          const url = window.URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', '学生信息表.xlsx');
          document.body.appendChild(link);
          link.click();
        } catch (error) {
          console.log(error);
        }
      },
      async changePage(currentPage) {
        this.currentPage = currentPage;
        await this.queryStudents();
      },
      async deleteStudent(student) {
        try {
          await this.$confirm(`确认删除学号为 ${student.sid} 的学生信息吗？`, '删除学生信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          });
          await this.$axios.delete(`/deleteStudent/${student.sid}`);
          this.$message.success('删除成功');
          await this.queryStudents();
        } catch (error) {
          console.log(error);
          this.$message.error('删除失败');
        }
      },
      //编辑
      handleEdit(row) {
        this.dialogFormVisible = true;
        Object.assign(this.editStudent, row); // 克隆当前行数据
      },
      //提交编辑
      handleEditStudent() {
        this.$axios.put(`/editStudent`, {
          sid: this.editStudent.sid,
          sname: this.editStudent.sname,
          sage: this.editStudent.sage,
          ssex: this.editStudent.ssex
        }).then(response => {
          const { code, message } = response.data;
          if (code === 200) {
            this.dialogFormVisible = false;// 隐藏编辑页面
            this.$message({
              message: "修改成功",
              type: 'success'
            });
            this.queryStudents();
          } else {
            this.$message.error(message);
          }
        }).catch(error => {
          console.log(error);
          // this.$message.error('修改失败');
        });
      },
      handleSubmit() {
        this.dialogVisible = false;
        // 其他逻辑
      },
      handleCancel() {
        // 处理子组件取消事件
        this.dialogVisible = false;
  
      }
    }
  }
  </script>
  <style src="@/style/index.css"></style> 